/**
 * 
 */
package com.lancoo.tyjx.multichatroom.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.lancoo.tyjx.multichatroom.R;
import com.lancoo.tyjx.multichatroom.constants.Constants;
import com.lancoo.tyjx.multichatroom.model.User;
import com.lancoo.tyjx.multichatroom.utils.MyAndroidUtil;
import com.lancoo.tyjx.multichatroom.utils.Tool;
import com.lancoo.tyjx.multichatroom.xmpp.XmppLoadThread;
import com.lancoo.tyjx.multichatroom.xmpp.XmppTool;

import org.jivesoftware.smack.packet.IQ;

/**
 * @author MZH
 *
 */
public class RegActivity extends Activity implements OnClickListener{
	private  TextView nameText,pwdText,pwdText1,emailText;//,emailText,phoneText
	private Button registBtn;
	private String name,pwd;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.acti_register);
		initView();
	}
	
	private void initView() {
		nameText=(TextView) findViewById(R.id.nameText);
		pwdText=(TextView) findViewById(R.id.pwdText);
		pwdText1=(TextView) findViewById(R.id.pwdText1);
		emailText=(TextView) findViewById(R.id.emailText);
		registBtn=(Button) findViewById(R.id.registBtn);
		registBtn.setOnClickListener(this);
		
	}

	public void onClick(View v) {
//		switch (v.getId()) {
//		case R.id.registBtn:
//			name = nameText.getText().toString();
//			pwd = pwdText.getText().toString();
//			String againPwd = pwdText1.getText().toString();
//			if (TextUtils.isEmpty(name)) {
//				Tool.initToast(RegActivity.this, getString(R.string.register_name));
//			}
//			else if (TextUtils.isEmpty(pwd)) {
//				Tool.initToast(RegActivity.this, getString(R.string.register_password));
//			} else if (TextUtils.isEmpty(againPwd)) {
//				Tool.initToast(RegActivity.this, getString(R.string.register_again_password));
//			} else if (!againPwd.equals(pwd)) {
//				Tool.initToast(RegActivity.this, getString(R.string.register_password_defferent));
//			} else if (emailText.equals("") || !Util.getInstance().isEmail(emailText.getText().toString())) {
//				Tool.initToast(RegActivity.this, getString(R.string.register_email_error));
//			}
//			else {
//				createAccount(name, pwd);
//			}
//			break;
//
//		default:
//			break;
//		}
	}
	
	
	private void createAccount(final String userName, final String passWord) {
		new XmppLoadThread(this) {

			@Override
			protected Object load() {
				IQ result = null;
				try {
					result = XmppTool.getInstance().regist(userName, passWord);
					if (result!=null && result.getType() == IQ.Type.RESULT) {
						XmppTool.getInstance().closeConnection();
						//完善信息
						Constants.loginUser = new User();
						Constants.loginUser.username = name;
						
//						VCard vcard = new VCard();
//						vcard.setField("email", emailText.getText().toString());
//						XmppTool.getInstance().login(name, pwd);
//						Constants.loginUser = new User(XmppTool.getInstance().getUserInfo(null));
//						Constants.loginUser.email = emailText.getText().toString();
//						XmppTool.getInstance().changeVcard(vcard);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}

			@Override
			protected void result(Object object) {
				IQ result = (IQ) object;
				if (result == null) {
					Tool.initToast(getApplicationContext(), getString(R.string.service_result));
				} 
				else if (result.getType() == IQ.Type.ERROR) {
					if (result.getError().toString().equalsIgnoreCase("conflict(409)")) {
						Tool.initToast(getApplicationContext(), getString(R.string.accounted));
					} else {
						Tool.initToast(getApplicationContext(), getString(R.string.register_fail));
					}
				} else if (result.getType() == IQ.Type.RESULT) {
					Tool.initToast(getApplicationContext(), getString(R.string.register_success));
					MyAndroidUtil.editXmlByString(Constants.LOGIN_ACCOUNT, name);
					MyAndroidUtil.editXmlByString(Constants.LOGIN_PWD, pwd);
					MyAndroidUtil.editXml(Constants.LOGIN_CHECK, true); 
					
//					Intent intent = new Intent(RegActivity.this, MainActivity.class);
//					intent.putExtra("isFromReg", true);
//					startActivity(intent); 
					finish();
				}
			}

		};

	}
}
